apiready = function() {
    seajs.use(['../../V', "../../api", "../../jquery", "../../app", "../../service"], function(V, $api, $, $app, service) {
        var vm = {
            el: '.container',
            data: {
                reasonList: '',
                reasonIndex: -1, //--理由选中
                goodsList: '',
                info: {
                    unionid: $api.getStorage("shop_unionid"),
                    order_id: '',
                    order_return_reason_id: '', //--退款理由id
                    content: '', //--退款理由
                    remark: '', //--备注
                    type: 0, //类型，1为退款，2为退货
                    refund_images: '',
                    order_return_id: '', //退款ID，若传入此值就是修改申请
                    goods_id: '',
                    goods_state: '' //货物状态
                },
                imgList: [],
                is_group: false,
                goodsLength: 0,
                name: '',
                pname: '', //--当前窗口名字
                reasonType: -1, //--获取退款理由方式
                stateIndex: -1 //--货物状态选中
            },
            methods: {
                init: function(et) {
                    //--order_return_id不为空是，获取退款信息
                    if (this.info.order_return_id != '') {
                        this.getRefundInfo();
                    }
                    //--获取退款商品信息
                    this.getOrderInfo();
                    //--获取退款理由
                    if (this.info.type == 2) {
                        this.reasonType = 1;
                        this.getRefundReason();
                    }
                },
                getOrderInfo: function(et) {
                    var self = this;
                    $app.ajax(service.buyGetOrderDetail, {
                        data: {
                            values: {
                                unionid: self.info.unionid,
                                order_id: self.info.order_id
                            }
                        }
                    }, function(res) {
                        console.log("refundApply: " + JSON.stringify(res));
                        if (Array.isArray(res)) {
                            self.goodsList = res;
                            self.goodsLength = res.length;
                        } else {
                            if (res.code == 0) {
                                self.goodsList = res.data;
                                self.goodsLength = res.data.goods.length;
                                var id = [];
                                for (var i = 0; i < res.data.goods.length; i++) {
                                    id.push(res.data.goods[i].goods_id);
                                }
                                self.info.goods_id = id.join(",");
                                if (self.goodsList.order_state == 2) {
                                    self.reasonType = 2;
                                    self.getRefundReason();
                                }
                            }
                        }
                    });
                },
                //--获取退款信息
                getRefundInfo: function() {
                    var self = this;
                    $app.ajax(service.buyGetRefundInfo, {
                        data: {
                            values: {
                                unionid: self.info.unionid,
                                order_return_id: self.info.order_return_id
                            }
                        }
                    }, function(res) {
                        //console.log("refundApply: "+JSON.stringify(res));
                        if (res.code == 0 && res.data != '') {
                            self.info.order_return_reason_id = res.data.order_return_reason_id;
                            self.info.content = res.data.content || '';
                            self.info.refund_images = res.data.refund_images;
                            self.info.order_return_id = res.data.order_return_id;
                            self.info.remark = res.data.remark || '';
                            self.info.type = res.data.type;
                            self.imgList = res.data.refund_images || [];
                        }
                    });
                },
                //--获取退款原因
                getRefundReason: function(et) {
                    var self = this;
                    $app.ajax(service.getOrderReturnReasonList, {
                        data: {
                            values: {
                                unionid: $api.getStorage('shop_unionid'),
                                type: self.reasonType,
                                order_id: self.info.order_id,
                                bs: ''
                            }
                        }
                    }, function(res) {
                        console.log("refundApply: " + JSON.stringify(res));

                        function reasonTxt(arr) {
                            for (var i = 0; i < arr.length; i++) {
                                if (arr[i].order_return_reason_id == self.info.order_return_reason_id) {
                                    self.$refs.selectRefundReason.querySelectorAll("i")[0].innerHTML = arr[i].content;
                                    break;
                                }
                            }
                        }
                        if (Array.isArray(res)) {
                            self.reasonList = res;
                            reasonTxt(res);
                        } else {
                            if (res.code == 0) {
                                self.reasonList = res.data;
                                reasonTxt(res.data);
                            }
                        }
                    });
                },
                //--显示退款理由
                selectRefundReason: function(et) {
                    if (this.info.type == 1 && this.reasonType == -1) {
                        $app.toast("请选择货物状态！");
                        return;
                    }
                    this.$refs.refundQuestion.style.display = "block";
                },
                //--选择退款理由
                liSelectReason: function(et) {
                    this.reasonIndex = +et.currentTarget.dataset.index;
                },
                //--获取退款理由
                getSelectReason: function(et) {
                    var li = this.$refs.refundQuestion.querySelectorAll("input");
                    for (var i = 0; i < li.length; i++) {
                        if (li[i].checked) {
                            if (i == li.length - 1) {
                                this.info.order_return_reason_id = li[i].dataset.id;
                                this.info.content = this.$refs.refundQuestion.querySelectorAll("textarea")[0].value || '';
                                break;
                            } else {
                                this.info.order_return_reason_id = li[i].dataset.id;
                                this.info.content = li[i].dataset.content;
                                break;
                            }
                        }
                    }
                    if (this.info.content == '') {
                        $app.toast('请填写退款退货理由！');
                        return;
                    }
                    this.$refs.refundQuestion.style.display = "none";
                    this.$refs.selectRefundReason.querySelectorAll("i")[0].innerHTML = this.info.content;
                },
                //--关闭选择框
                closeElectReason: function() {
                    this.$refs.refundQuestion.style.display = "none";
                },
                imgUpload: function() {
                    var self = this;
                    if (this.imgList.length >= 6) {
                        $app.toast('最多选择6张图片！');
                        return;
                    }
                    $app.getPictureS(6, this.imgList.length, service['fileupload'], function(res) {
                        if (res.length > 0) {
                            for (var i = 0; i < res.length; i++) {
                                self.imgList.push(res[i]);
                            }
                        } else {
                            $app.toast('图片上传失败，请重新上传');
                        }
                    });
                },
                //--删除图片
                deleteImg: function(et) {
                    var index = +et.currentTarget.dataset.index;
                    this.imgList.splice(index, 1);
                },
                //--提交退款信息
                submitRefundInfo: function(et) {
                    var self = this;
                    this.info.refund_images = this.imgList.join(",");
                    this.info.remark = this.$refs.infoRemark.value || "";
                    if (this.info.type == 1 && this.reasonType == -1) {
                        $app.toast("请选择货物状态！");
                        return;
                    }
                    if (this.info.order_return_reason_id == '') {
                        $app.toast('请选择退款理由！');
                        return;
                    }
                    if (this.imgList.length <= 0 && (this.info.type == 2 || this.reasonType == 1)) {
                        $app.toast("请上传凭证...");
                        return;
                    }
                    $app.ajax(service.buyRefundOrder, {
                        data: {
                            values: self.info
                        }
                    }, function(res) {
                        //console.log("submit_refundApply: "+JSON.stringify(res));
                        if (res.code == 0) {
                            self.info.order_return_id = +res.data;
                            $app.toast("退款信息提交成功");
                            if (self.pname == 'buyRefundDetail') {
                                api.closeWin();
                            } else {
                                $app.openWin("buyRefundDetail", {
                                    order_return_id: self.info.order_return_id,
                                    order_id: self.info.order_id,
                                    name: self.name
                                });
                            }
                        }
                    });
                },
                openSelectGoodsState: function(et) {
                    this.$refs.refundGoodsState.style.display = 'block';
                },
                //--确定选择货物状态
                selectGoodsStateSuer: function(et) {
                    var li = this.$refs.refundGoodsState.querySelectorAll("input:checked");
                    if (li.length <= 0) {
                        $app.toast("请选择状态！");
                        return;
                    }
                    var txt = li[0].dataset.txt;
                    var index = +li[0].dataset.index;
                    this.$refs.selectGoodsStateLi.querySelectorAll("i")[0].innerHTML = txt;
                    this.info.goods_state = txt;
                    this.reasonType = index;
                    this.reasonIndex = -1;
                    this.info.content = '';
                    this.info.order_return_reason_id = '';
                    this.closeSelectGoodsState();
                    this.getRefundReason();
                },
                //--关闭货物状态选择
                closeSelectGoodsState: function(et) {
                    this.$refs.refundGoodsState.style.display = 'none';
                },
                //--选中货物状态
                selectState: function(et) {
                    this.stateIndex = +et.currentTarget.dataset.index;
                }
            },
            mounted: function() {

            },
            created: function() {
                this.info.type = api.pageParam.type || '';
                this.info.order_return_id = api.pageParam.order_return_id || '';
                this.info.order_id = api.pageParam.order_id;
                this.is_group = api.pageParam.is_group;
                this.name = api.pageParam.name;
                this.pname = api.pageParam.prve;
                this.init();
            }
        };
        //var shopVue = new Vue(vm);
        var even = function() {

        };
        var newVue = new Vue(vm);
        even();

    })
}
